﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BdClient;
using Univer.Model;

namespace Univer
{
    public partial class CarsForm : Form
    {
        private readonly BdExecute _execute;
        private decimal countResults = 0;
        public CarsForm()
        {
            _execute = new BdExecute();
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            countResults = 0;
            var table = new DataTable();
            table.Columns.Add("id");
            table.Columns.Add("Марка");
            table.Columns.Add("Цена");
            table.Columns.Add("Аренда в день");
           
            if (radioButton1.Checked)
            {
                foreach (var car in _execute.GetCars())
                {
                    countResults++;
                    table.Rows.Add(car.id,
                        car.Name, car.Price,
                        car.PriceDay);
                }
            }
            else if (radioButton2.Checked)
            {
                foreach (var car in _execute.GetCarsBusy(DateTime.Now))
                {
                    countResults++;
                    table.Rows.Add(car.id,
                        car.Name, car.Price,
                        car.PriceDay);
                }
            }
            else if (radioButton3.Checked)
            {
                foreach (var car in _execute.GetCarsPopular())
                {
                    countResults++;
                    table.Rows.Add(car.id,
                        car.Name, car.Price,
                        car.PriceDay);
                }
            }
            
            
            dataGridView1.DataSource = table;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int count = dataGridView1.SelectedRows.Count;
            if (count == 0 || countResults == 0)
            {
                MessageBox.Show("Не выбрано ни одного автомобиля");
            }
            else if (count > 1)
            {
                MessageBox.Show("Выбрано более одного автомобиля");
            }
            else
            {
                Car car = new Car();
                car.id = System.Convert.ToDecimal(dataGridView1.SelectedRows[0].Cells[0].Value);
                car.name = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                car.price = System.Convert.ToDecimal(dataGridView1.SelectedRows[0].Cells[2].Value);
                car.priceDay = System.Convert.ToDecimal(dataGridView1.SelectedRows[0].Cells[3].Value);
                Manager.getInstance().selectedCar = car;
                Manager.getInstance().mainFormCallback.updateCarInfo();
                Close();
            }
        }
    }
}
